﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CNotebook
{
    public class SeriesManager:ConnectionManager
    {
        public bool IsAutoNumber(string sDocType)
        {
            try
            {
                SqlCommand oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;
                oCommand.CommandType = CommandType.Text;

                oCommand.CommandText = "SELECT IsAuto FROM OBJTYPE WHERE DocType=@DocType";
                oCommand.Parameters.Add(new SqlParameter("@DocType", sDocType));

                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                return (bool)ds.Tables[0].Rows[0][0];


            }
            catch
            {
                return false;
            }
        }

        public string GetNewSeries(string sDocType)
        {
            try
            {
                SqlCommand oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;
                oCommand.CommandType = CommandType.Text;

                oCommand.CommandText = "SELECT DOCEXTENSION + SUBSTRING(DOCFORMAT, 0,(LEN(DOCFORMAT)-LEN(CONVERT(NVARCHAR(50), DOCFORMAT)))+1)  + CONVERT(NVARCHAR(50), SERIES) AS DOCUMENT FROM OBJTYPE WHERE DOCTYPE=@DOCTYPE";
                oCommand.Parameters.Add(new SqlParameter("@DocType", sDocType));

                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);


                oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;
                oCommand.CommandType = CommandType.Text;

                oCommand.CommandText = "UPDATE OBJTYPE SET SERIES=SERIES+1 WHERE DOCTYPE=@DocType";
                oCommand.Parameters.Add(new SqlParameter("@DocType", sDocType));
                oCommand.ExecuteNonQuery();

                return (string)ds.Tables[0].Rows[0][0];


            }
            catch
            {
                return null;
            }
        }

        public void RevertSeries(string sDocType)
        {
            try
            {
                SqlCommand oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;
                oCommand.CommandType = CommandType.Text;

                oCommand.CommandText = "SELECT DOCEXTENSION + SUBSTRING(FORMAT, 0,(LEN(DOCFORMAT)-LEN(CONVERT(NVARCHAR(50), SERIES)))+1)  + CONVERT(NVARCHAR(50), SERIES) AS DOCUMENT FROM OBJTYPE WHERE DOCTYPE=@DOCTYPE";
                oCommand.Parameters.Add(new SqlParameter("@DocType", sDocType));
                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);


                oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;
                oCommand.CommandType = CommandType.Text;

                oCommand.CommandText = "UPDATE OBJTYPE SET SERIES=SERIES-1 WHERE DOCTYPE=@DOCTYPE";
                oCommand.Parameters.Add(new SqlParameter("@DocType", sDocType));
                oCommand.ExecuteNonQuery();




            }
            catch
            {

            }
        }
    }
}
